//
//  HSObjectiveView.swift
//  HomeSchoolDemo
//
//  Created by rimi on 2016/10/15.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit

class HSObjectiveView: UIView {
    
    var model:HSHW_htViewModel? {
        didSet {
            setupUI()
            
        }
    }
    
    fileprivate func setupUI() {
        let a = model?.details.ht_type ?? 0
        switch a {
        case 1:
            if let details = model?.details {
                let arr = details.ht_option as! [HSObjectiveOptionModel]
                for (i,oModel) in arr.enumerated() {
                    let firstStr = (oModel.ho_text! as NSString).substring(to: 1)
                    if firstStr == (details.ht_correctWord ?? "") {
                        let bgView = options(leftImage: "ac_radio_unchecked", leftImage2: "ac_radio_checked", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                        addSubview(bgView)
                    } else {
                        let bgView = options(leftImage: "ac_radio_unchecked", leftImage2: "", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                        addSubview(bgView)
                    }
                    
                }
                
            }
            
        case 2:
            if let details = model?.details {
                let arr = details.ht_option as! [HSObjectiveOptionModel]
                for (i,oModel) in arr.enumerated() {
                    let firstStr = (oModel.ho_text! as NSString).substring(to: 1)
                    let charArr = details.ht_correctWord ?? ""
                    let count = (charArr as NSString).length
                    
                    for index in 1...count {
                        let oneC = (charArr as NSString).substring(with: NSMakeRange(index - 1, 1))
                        if firstStr == oneC {
                            let bgView = options(leftImage: "ac_checked_no", leftImage2: "ac_checked_gray_yes", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                            addSubview(bgView)
                        } else {
                            let bgView = options(leftImage: "ac_checked_no", leftImage2: "", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                            addSubview(bgView)
                        }
                        
                    }
                    
                    
                }
                
            }
            
        case 3:
            if let details = model?.details {
                let arr = details.ht_option as! [HSObjectiveOptionModel]
                for (i,oModel) in arr.enumerated() {
                    let firstStr = (oModel.ho_text! as NSString).substring(to: 1)
                    if firstStr == (details.ht_correctWord ?? "") {
                        let bgView = options(leftImage: "ac_radio_unchecked", leftImage2: "ac_radio_checked", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                        addSubview(bgView)
                    } else {
                        let bgView = options(leftImage: "ac_radio_unchecked", leftImage2: "", content: oModel.ho_text ?? "", frame: CGRect(x: 20, y: i * 25, width: Int(screenW * 0.8), height: 20))
                        addSubview(bgView)
                    }
                    
                }
                
            }
            
        case 4:
            print("主观")
        default:
            break
        }
        
    }
    
    func options(leftImage:String,leftImage2:String,content:String,frame:CGRect) -> UIView {
        let bgView = UIView(frame: frame)
        
        let imageView1 = UIImageView(frame: CGRect(x: 0, y: 2, width: 16, height: 16))
        imageView1.image = UIImage(named: leftImage)
        bgView.addSubview(imageView1)
        
        let imageView2 = UIImageView(frame: CGRect(x: 4, y: 6, width: 8, height: 8))
        imageView2.image = UIImage(named: leftImage2)
        bgView.addSubview(imageView2)
        
        let contentLabel = UILabel(frame: CGRect(x: 25, y: 0, width: frame.size.width - 20, height: 20))
        contentLabel.text = content
        contentLabel.textAlignment = NSTextAlignment.left
        bgView.addSubview(contentLabel)
        
        return bgView
    }
    
}
